namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class OrganJobPosistionBLL
    {
        public int Add(OrganJobPosistion model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into OrganJobPosistion(");
            builder.Append("OrganName,PositionID,Count");
            builder.Append(") values (");
            builder.Append("@OrganName,@PositionID,@Count");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrganName", model.OrganName), new SqlParameter("@PositionID", model.PositionID), new SqlParameter("@Count", model.Count) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int CountID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrganJobPosistion ");
            builder.Append(" where CountID=@CountID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CountID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CountID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string CountIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrganJobPosistion ");
            builder.Append(" where ID in (" + CountIDlist + ")  ");
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<OrganJobPosistion_Count> GetCountList(int OrganID)
        {
            return new List<OrganJobPosistion_Count>();
        }

        public List<OrganJobPosistion> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM OrganJobPosistion ");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            OrganJobPosistion item = null;
            List<OrganJobPosistion> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<OrganJobPosistion>();
                foreach (DataRow row in table.Rows)
                {
                    item = new OrganJobPosistion();
                    if (row["CountID"].ToString() != "")
                    {
                        item.CountID = int.Parse(row["CountID"].ToString());
                    }
                    item.OrganName = row["OrganName"].ToString();
                    item.PositionID = row["PositionID"].ToString();
                    if (row["Count"].ToString() != "")
                    {
                        item.Count = int.Parse(row["Count"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public OrganJobPosistion GetModel(int CountID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select CountID, OrganName, PositionID, Count  ");
            builder.Append("  from OrganJobPosistion ");
            builder.Append(" where CountID=@CountID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CountID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CountID;
            OrganJobPosistion posistion = new OrganJobPosistion();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["CountID"].ToString() != "")
                {
                    posistion.CountID = int.Parse(row["CountID"].ToString());
                }
                posistion.OrganName = row["OrganName"].ToString();
                posistion.PositionID = row["PositionID"].ToString();
                if (row["Count"].ToString() != "")
                {
                    posistion.Count = int.Parse(row["Count"].ToString());
                }
                return posistion;
            }
            return null;
        }

        public bool Update(OrganJobPosistion model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update OrganJobPosistion set ");
            builder.Append(" OrganName = @OrganName , ");
            builder.Append(" PositionID = @PositionID , ");
            builder.Append(" Count = @Count  ");
            builder.Append(" where CountID=@CountID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CountID", model.CountID), new SqlParameter("@OrganName", model.OrganName), new SqlParameter("@PositionID", model.PositionID), new SqlParameter("@Count", model.Count) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

